<?php
/**
 * @title 147. 对链表进行插入排序 - 排序
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param ListNode $head
     * @return ListNode
     */
    function insertionSortList($head) {
        if($head === null){
            return null;
        } else {}

        /**
         * @since 2020-08-07 取值
         */
        $nums = [];
        while($head !== null){
            $nums[] = $head->val;
            $head = $head->next;
        }

        /**
         * @since 2020-08-07 排序
         */
        sort($nums);

        /**
         * @since 2020-08-07 链表重建
         */
        $head = new ListNode($nums[0]);
        $next = $head;
        for($i=1, $len=count($nums); $i<$len; $i++){
            $next->next = new ListNode($nums[$i]);
            $next = $next->next;
        }

        /**
         * @return
         */
        return $head;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202008/2020.08.07_3.php
 */
$datas = [
    [4,2,1,3],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->insertionSortList($xhprof->listNode($data));
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
